<template>
  <div class="pad-20 bgc-default">
    <div class="pad-20 bg-color-white">
      <div>
        <h2>
          <span>授权商品设置：</span>
          <el-radio-group v-model="authorizeTypeRadio" @change="radioChange">
            <!-- <el-radio :label="0">授权所有商品</el-radio> -->
            <el-radio :label="1">授权以下商品</el-radio>
          </el-radio-group>
        </h2>
      </div>

      <div v-show="authorizeTypeRadio == 1">
        <div>
          <el-button type="text" style="margin: 0 30px 0 150px" @click="doAddGoods()">添加商品</el-button>
          <el-button type="text" @click="doRemoveGoods('')">移除选中商品</el-button>
        </div>
        <!--顶部搜索 -->
        <div class="top-operate mar-t-20">
          <div class="search-item">
            <el-input @keyup.enter.native="handleSearch" v-model.trim="searchListQuery.title" placeholder="商品名称"
              style="width: 200px"></el-input>
            <el-cascader v-model="categoryIds" placeholder="商品零售分类" :props="{ checkStrictly: true }"
              :options="categoryData" filterable clearable style="width: 250px"></el-cascader>
            <el-select v-model="searchListQuery.brandId" placeholder="商品品牌" clearable style="width: 120px">
              <el-option v-for="item in listBrands" :key="item.brandId" :label="item.brandName" :value="item.brandId">
              </el-option>
            </el-select>
            <el-select v-model="searchListQuery.goodsType" placeholder="商品类型" clearable style="width: 150px">
              <el-option label="零售商品" value="30"></el-option>
              <el-option label="批发商品" value="31"></el-option>
              <el-option label="零售+批发商品" value="32"></el-option>
            </el-select>
            <el-select v-model="searchListQuery.goodsStatus" placeholder="上下架状态" clearable style="width: 150px">
              <el-option label="上架" value="1"></el-option>
              <el-option label="下架" value="0"></el-option>
            </el-select>
            <el-button-group class="mar-l-20">
              <el-button type="primary" @click="handleSearch">查询</el-button>
              <el-button plain @click="resetSearch">重置</el-button>
            </el-button-group>
          </div>
        </div>
        <!-- 内容列表 -->
        <div class="table-container">
          <el-table :data="dataList" @selection-change="handleSelectionChange" v-loading="listLoading"
            element-loading-text="加载中">
            <el-table-column width="50px" type="selection"></el-table-column>
            <el-table-column min-width="320px" label="商品名称" prop="title">
              <template slot-scope="scope">
                <div class="v-align-c cursor-p">
                  <label><img :src="scope.row.mainImage" class="user-avatar"></label>
                  <div class="pad-l-10">
                    <div class="line-2 width-300">
                      <el-tag size="mini" v-if="scope.row.skuFlag == 1" type="danger" effect="light">多</el-tag>
                      <span class="font-blue">{{scope.row.title}}</span>
                    </div>
                    <div>
                      <span class="font-color-6 font-13"
                        v-if="scope.row.categoryName">[{{ scope.row.categoryName }}]</span>
                    </div>
                  </div>
                </div>
              </template>
            </el-table-column>
            <el-table-column min-width="100px" label="上下架状态" prop="goodsStatus">
              <template slot-scope="scope">
                <span v-if="scope.row.goodsType === 30">
                  <span>{{dict.goodsStatus[scope.row.goodsStatus]}}</span>
                </span>
                <span v-else-if="scope.row.goodsType === 31">
                  <span>{{dict.goodsStatus[scope.row.wholesaleGoodsStatus]}}</span>
                </span>
                <span v-else-if="scope.row.goodsType === 32">
                  <span>{{dict.goodsStatus[scope.row.wholesaleGoodsStatus || scope.row.goodsStatus]}}</span>
                </span>
              </template>
            </el-table-column>
            <el-table-column min-width="120px" label="商品品牌" prop="brandName">
            </el-table-column>
            <el-table-column min-width="120px" label="商品类型" prop="goodsType">
              <template slot-scope="scope">
                {{dict.goodsType[scope.row.goodsType]}}
              </template>
            </el-table-column>
            <el-table-column min-width="150px" label="操作" align="right">
              <template slot-scope="scope">
                <el-button type="text" size="medium" @click="doRemoveGoods(scope.row)">移除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <!-- 列表分页 -->
        <div class="pagination">
          <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="searchListQuery.currentPage" :page-sizes="[10, 20, 50, 100]"
            :page-size="searchListQuery.size" layout="total, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>
        </div>
      </div>
    </div>

    <div class="text-align-c pad-t-30 pad-b-30">
      <el-button type="primary" @click="doSave()">保存</el-button>
      <el-button @click="$router.go(-1)">返回</el-button>
    </div>

    <el-dialog title="选择商品" :visible.sync="selectGoodsVisible" width="1200px">
      <div class="scroll-layer-box">
        <goods-select :showFlag="selectGoodsVisible" :goodsSource="3" :warehouseId="warehouseId" @refresh="initList"
          @closeDialog="doCloseGoodsSelect"></goods-select>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import goodsSelect from './goods-select'
  import {
    listBrand
  } from '@/api/user/enterpriseBrand'
  import {
    listGoodsCategory
  } from '@/api/goods/goods'
  import {
    getDictByGroupName
  } from '@/api/common/common'
  import {
    updateAuthorizeType
  } from '@/api/warehouse/warehouseInfo'
  import {
    pagingWarehouseGoods,
    doRemoveGoods
  } from '@/api/warehouse/warehouseGoods'

  export default {
    components: {
      goodsSelect
    },
    data() {
      return {
        // 查询表单
        searchListQuery: {
          currentPage: 1,
          size: 10,
          title: '',
          brandId: '',
          goodsType: '',
          goodsStatus: ''
        },
        // 数据字典
        dict: {
          goodsStatus: {},
          goodsType: {}
        },
        // 仓库id
        warehouseId: '',
        // 分类model
        categoryIds: [],
        // 授权类型radio
        authorizeTypeRadio: '',
        // 品牌集合
        listBrands: [],
        // 商品零售分类集合
        categoryData: [],
        // 勾选集合
        multipleSelection: [],
        dataList: [],
        total: 0,
        listLoading: true,
        // 选择商品dialog
        selectGoodsVisible: false
      }
    },
    // 初始化页面
    created() {
      const query = this.getCurrentPage().query
      this.warehouseId = query.warehouseId
      if (query.authorizeType >= 0) {
        this.authorizeTypeRadio = query.authorizeType
        if (this.authorizeTypeRadio === 1 || this.authorizeTypeRadio === '1') {
          this.initList()
        }
      }
      // 获取品牌
      listBrand(this.getEnterpriseId()).then(response => {
        this.listBrands = response.data
      })
      // 获取商品零售分类
      listGoodsCategory(this.getEnterpriseId()).then(response => {
        this.categoryData = response.data
      })
      // 初始化数据字典
      getDictByGroupName('ProjectGoodsDTO').then(response => {
        this.dict = response.data
      })
    },
    methods: {
      // radio切换事件
      radioChange(val) {
        if (val === 1) {
          this.initList()
        }
      },
      // 初始化
      initList() {
        this.listLoading = true
        this.searchListQuery.authorizeStatus = 1
        this.searchListQuery.warehouseId = this.warehouseId
        this.searchListQuery.oneCategoryId = this.categoryIds.length > 0 ? this.categoryIds[0] : ''
        this.searchListQuery.twoCategoryId = this.categoryIds.length > 1 ? this.categoryIds[1] : ''
        this.searchListQuery.threeCategoryId = this.categoryIds.length > 2 ? this.categoryIds[2] : ''
        pagingWarehouseGoods(this.searchListQuery).then(response => {
          if (response.isError) return
          this.dataList = response.data.records
          this.total = response.data.total
          this.listLoading = false
        })
      },
      // 查询事件
      handleSearch() {
        this.searchListQuery.currentPage = 1
        this.initList()
      },
      // 重置事件
      resetSearch() {
        const originData = this.$options.data()
        this.categoryIds = originData.categoryIds
        this.searchListQuery = originData.searchListQuery
        this.initList()
      },
      // 保存事件
      doSave() {
        if (this.authorizeTypeRadio !== 0 && this.authorizeTypeRadio !== 1) {
          this.$message({
            type: 'error',
            message: '请选择授权商品设置'
          })
          return
        }
        const form = {
          warehouseId: this.warehouseId,
          authorizeType: this.authorizeTypeRadio
        }
        updateAuthorizeType(form).then(response => {
          if (response.isError) return
          this.$router.go(-1)
          this.$message({
            type: 'success',
            message: '保存成功'
          })
        })
      },
      // 添加商品事件 打开dialog
      doAddGoods() {
        this.selectGoodsVisible = true
      },
      // 关闭商品选择dialog
      doCloseGoodsSelect() {
        this.selectGoodsVisible = false
      },
      // 移除商品
      doRemoveGoods(row) {
        let form
        if (row) {
          form = {
            warehouseId: this.warehouseId,
            goodsId: row.goodsId,
            enterpriseId: this.getEnterpriseId(),
            warehouseGoodsId: row.warehouseGoodsId
          }
        } else {
          if (this.multipleSelection.length <= 0) {
            this.$message({
              message: '请勾选商品',
              type: 'warning'
            })
            return
          }
          form = {
            warehouseId: this.warehouseId,
            goodsIds: this.multipleSelection.map(item => item.goodsId),
            warehouseGoodsIds: this.multipleSelection.map(item => item.warehouseGoodsId)
          }
        }
        this.listLoading = true
        doRemoveGoods(form).then(response => {
          this.listLoading = false
          if (response.isError) return
          this.initList()
          this.$message({
            type: 'success',
            message: '移除成功'
          })
        })
      },
      // 更改当前页数
      handleCurrentChange(val) {
        this.searchListQuery.currentPage = val
        this.initList()
      },
      // 更改当前页面大小
      handleSizeChange(val) {
        this.searchListQuery.size = val
        this.initList()
      },
      // 勾选事件
      handleSelectionChange(val) {
        this.multipleSelection = val
      }
    }
  }
</script>

<style lang="less" scoped>
  .top-operate {
    margin-bottom: 20px;
    height: 85px;
    background-color: #f7f8fa;
    display: flex;
    align-items: center;
    white-space: nowrap;
    flex-wrap: nowrap;
    width: 100%;
    overflow-x: auto;
  }

  .user-avatar {
    width: 68px;
    height: 68px;
    display: block;
    padding: 1px;
    border: #efefef 1px solid;
  }

  .search-item {
    flex-shrink: 0;
    padding: 0 20px;

    label {
      font-weight: unset;
    }
  }
</style>
